package com.zd.bms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zd.bms.bean.vo.req.WechatUserListReqVo;
import com.zd.bms.bean.vo.resp.WechatUserRespVo;
import com.zd.bms.service.IScenicSpotService;
import com.zd.bms.service.IUserInfoService;
import com.zd.common.bean.model.ScenicSpot;
import com.zd.common.bean.model.UserInfo;
import com.zd.common.bean.model.UserReceiveInfo;
import com.zd.common.common.utils.DateTimeUtil;
import com.zd.common.common.utils.LogUtils;
import com.zd.common.dao.UserInfoDao;
import com.zd.common.dao.UserReceiveInfoDao;
import com.zd.common.dao.common.BaseDao;
import com.zd.common.dao.common.FindRequestEntity;
import com.zd.common.dao.common.OrderEnum;
import com.zd.common.dao.common.Page;
import com.zd.common.dao.common.WhereEnum;
import com.zd.common.service.impl.BaseServiceImpl;

@Service
public class UserInfoServiceImpl extends BaseServiceImpl<UserInfo> implements IUserInfoService {
	@Autowired
	private UserInfoDao userInfoDao;
	
	@Autowired
	private IScenicSpotService scenicSpotService;
	
	@Autowired
	private UserReceiveInfoDao userReceiveInfoDao;

	@Override
	public BaseDao<UserInfo> getBaseDao() {
		return userInfoDao;
	}
	
	
	public Page<WechatUserRespVo> pageWechatUser(WechatUserListReqVo req) throws Exception {
		FindRequestEntity findReq = new FindRequestEntity();
		if (req.getScenicSpotId() != null) {
			findReq.addWhere("scenic_spot_id", WhereEnum.EQ, req.getScenicSpotId());
		}
		findReq.addOrder("create_time", OrderEnum.DESC);

		Page<UserInfo> userInfoPage = page(findReq);
		Page<WechatUserRespVo> resultPage = new Page<>();
		resultPage.setPage(userInfoPage.getPage());
		resultPage.setPageSize(userInfoPage.getPageSize());
		resultPage.setTotalRow(userInfoPage.getTotalRow());
		List<UserInfo> list = userInfoPage.getList();
		List<WechatUserRespVo> resultVoList = new ArrayList<>();

		//获取景区
		Map<Integer, ScenicSpot> scenicSpotMap = new HashMap<>();
		for (UserInfo userInfo : list) {
			WechatUserRespVo respVo = new WechatUserRespVo();
			BeanUtils.copyProperties(userInfo, respVo);

			//获取景区信息
			ScenicSpot scenicSpot = scenicSpotMap.get(userInfo.getScenicSpotId());
			if (scenicSpot == null) {
				scenicSpot = scenicSpotService.get(userInfo.getScenicSpotId());
				scenicSpotMap.put(userInfo.getScenicSpotId(), scenicSpot);
			}
			if (scenicSpot != null) {
				respVo.setScenicSpotName(scenicSpot.getName());
			}
			
			//获取收件人信息
			findReq = new FindRequestEntity();	
			findReq.addWhere("user_info_id", WhereEnum.EQ, userInfo.getId());
			findReq.addOrder("create_time", OrderEnum.DESC);
			findReq.setPage(0, 1);
			List<UserReceiveInfo> receiveInfoList = userReceiveInfoDao.find(findReq);
			if(receiveInfoList != null && receiveInfoList.size() > 0) {
				UserReceiveInfo receiveInfo = receiveInfoList.get(0);
				BeanUtils.copyProperties(receiveInfo, respVo);
			}
			
			resultVoList.add(respVo);
		}
		resultPage.setList(resultVoList);
		return resultPage;
	}


	@Override
	public Map<String, Object> statisticalUser(WechatUserListReqVo req) throws Exception {
		Map<String, Object> resultMap =  new HashMap<>();
		FindRequestEntity findReq = new FindRequestEntity();
		if (req.getScenicSpotId() != null) {
			findReq.addWhere("scenic_spot_id", WhereEnum.EQ, req.getScenicSpotId());
		}
		Integer totalNum = userInfoDao.count(findReq.getWhereList());
		Date todayTime = DateTimeUtil.string2Date(DateTimeUtil.date2String(new Date(), "yyyy-MM-dd")+" 00:00:00");
		LogUtils.info(todayTime);
		findReq.addWhere("create_time", WhereEnum.GE, todayTime);
		Integer todayNum = userInfoDao.count(findReq.getWhereList());
		LogUtils.info(totalNum + "        :        " + todayNum);
		resultMap.put("totalNum", totalNum);
		resultMap.put("todayNum", todayNum);
		return resultMap;
	}
}
